<template>
  <div>
    <!--搜索查询-->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline" style="float: left">
      <el-form-item>
         <el-input v-model="searchForm.sname" placeholder="门店名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerAdd">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="flush">刷新</el-button>
      </el-form-item>
    </el-form>


      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="provinceName" label="省" align="center" width="150">
        </el-table-column>
        <el-table-column prop="cityName" label="市" align="center" width="150">
        </el-table-column>
        <el-table-column prop="areaName" label="区县" align="center" width="150">
        </el-table-column>

        <el-table-column prop="sname" label="门店名称" align="center" width="150">
        </el-table-column>

        <el-table-column prop="ishot" width="150" label="是否常用" v-if="false">
        </el-table-column>

        <el-table-column prop="address" width="150" label="地址">
        </el-table-column>
        <el-table-column prop="longitude" width="100" label="经度">
        </el-table-column>
        <el-table-column prop="dimensionality" width="100" label="维度">
        </el-table-column>
        <el-table-column prop="addtime" width="150" label="添加时间">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>

      </el-table>

    <el-row>
      <!--分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[2, 3, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-row>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">

        <el-form-item label="门店地址" :label-width="formLabelWidth">

          <el-select class="arriveClass" v-model="form.provinceId" placeholder="选择省" @change="provinceChange">
            <el-option v-for="item in provinceList" :key="item.baseAreaid" :label="item.name" :value="item.baseAreaid"></el-option>
          </el-select>
          <el-select class="arriveClass" v-model="form.cityId" placeholder="选择城市" @change="cityChange($event)">
            <el-option v-for="item in cityList" :key="item.baseAreaid" :label="item.name" :value="item.baseAreaid"></el-option>
          </el-select>
          <el-select class="arriveClass" v-model="form.areaId" placeholder="选择区县" @change=" Changea">
            <el-option v-for="item in countryList" :key="item.baseAreaid" :label="item.name" :value="item.baseAreaid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店名称" prop="sname" :label-width="formLabelWidth">
          <el-input v-model="form.sname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="address" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="longitude" :label-width="formLabelWidth">
          <el-input v-model="form.longitude" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="维度" prop="dimensionality" :label-width="formLabelWidth">
          <el-input v-model="form.dimensionality" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="添加时间" prop="addtime" :label-width="formLabelWidth">
          <el-date-picker v-model="form.addtime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择添加时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [], //权限树数组对象
      defaultProps: {
        //父对象传值
        children: "children",
        label: "label",
      },
      provinceList: [],
      cityList: [],
      countryList: [],
      nodeChecked: [], //勾选项目数组对象
      tableData: [],
      currentPage: 1, //页码 第几页
      pageSize: 5, //每页显示数量
      total: 100, //总条数
      searchForm: { }, //搜索form表单
      dialogFormVisible: false, //是否显示对话框  默认是不显示
      form: {}, //form表单对象
      menuTreeVisible: false, //权限树是否显示
      formLabelWidth: "120px",
      title: "门店添加",
    };
  },
  created() {
    //初始化方法 类似于jquery  $({})
    this.loadData();
    var _this = this;
    _this.$http
      .post("rent/basearea/list?parentid=" + 0)
      .then(function (response) {
        console.log(response);
        if (response.data.code == 2000) {
          _this.provinceList = response.data.data;

        }
      })
      .catch(function (error) {
        //错误的处理
        // handle error
        console.log(error);
      })
      .then(function () {
        //总执行 最终处理      类似java  异常处理的finally
        // always executed
      });
  },
  //所有函数
  methods: {
    //加载数据
    loadData() {
      var _this = this;
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      _this.$http
        .post("rent/shop/list?currentPage="+this.currentPage+"&pageSize="+this.pageSize,this.searchForm)
        .then(function (response) {
          console.log(response)
          if (response.data.code == 2000) {
            _this.tableData = response.data.data.list;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    Changea() {
      this.$forceUpdate()
    },
    //
    provinceChange(value) {
      //alert(val);
      //this.form.dictId;
      var _this = this;
      this.form.cityId = '';
      this.form.areaId = '';
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      _this.$http
        .post("rent/basearea/list?parentid=" + this.form.provinceId)
        .then(function (response) {
          if (response.data.code == 2000) {
            _this.cityList = response.data.data;

          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });

    },
    cityChange(value) {
      //alert(val);
      //this.form.dictId;
      var _this = this;
      this.form.areaId = '';
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      _this.$http
        .post("rent/basearea/list?parentid=" + this.form.cityId)
        .then(function (response) {
          if (response.data.code == 2000) {
            _this.countryList = response.data.data;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    //处理更新
    handleEdit(index, row) {
      var _this = this;
      //复制 $http官网代码
      // var token  = localStorage.getItem("token");
      _this.$http
        .post("rent/basearea/list?parentid=" + row.provinceId)
        .then(function (response) {
          if (response.data.code == 2000) {
            _this.cityList = response.data.data;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });

      // var token  = localStorage.getItem("token");
      _this.$http
        .post("rent/basearea/list?parentid=" + row.cityId)
        .then(function (response) {
          if (response.data.code == 2000) {
            _this.countryList = response.data.data;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
      //row  当前行数据 如果是第一行  row = {deptId:1,deptName:'管理部'把一行数据赋值给form赋值,description:'店长的事',status:1}
      console.log(index, row);
      //显示对话框
      _this.dialogFormVisible = true;
      //指定对话框标题
      _this.title = "门店更新";
      //把一行数据赋值给form赋值
      _this.form = row;
      //把数字1变为字符串1
      _this.form.ishot = row.ishot + "";
    },
    //处理添加
    handlerAdd() {
      this.imgUrl = "";
      //显示对话框
      this.dialogFormVisible = true;
      //指定对话框标题
      this.title = "门店添加";
      //让form表单重新赋空值
      this.form = {};
    },
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // alert(JSON.stringify(this.form));
        if (valid) {
          var url = "";
          if (this.title == "门店添加") {
            url = "rent/shop/insert";
          } else {
            url = "rent/shop/update";
          }
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 $http官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http
            .post(url, this.form) //指定请求的url及参数  get
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              console.log(JSON.stringify(response));
              //直接使用this 会指 $http对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 2000) {
                //关闭弹框
                _this.dialogFormVisible = false;
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //处理逻辑删除
    handleDelete(index, row) {
      console.log(index, row);
      //确认框提示
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 $http官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http
            .delete("rent/shop/delete?id=" + row.id) //指定请求的url及参数  get
            .then(function (response) {
              //正确返回对象的处理
              // handle success
              console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              console.log(JSON.stringify(response));
              //直接使用this 会指 $http对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 2000) {
                //提示添加成功
                _this.$message({
                  message: "恭喜你，这是一条成功消息",
                  type: "success",
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error("错了哦，这是一条错误消息");
              }
            })
            .catch(function (error) {
              //错误的处理
              // handle error
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //处理每页数量发生变化
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //让每页数量发生变化
      this.pageSize = val;
      //重载数据
      this.loadData();
    },
    //处理当前页变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //让每页数量发生变化
      this.currentPage = val;
      //重载数据
      this.loadData();
    },
    //查询
    search() {
      this.loadData();
    },
    //刷新
    flush() {
      this.searchForm = {};
      this.currentPage = 1;
      this.pageSize = 5;
      this.loadData();
    },
    //取消按钮
    dialogFormVisiblExit() {
      this.form = "";
      this.dialogFormVisibleAdd = false;
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 178px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
